package com.apotik.legenda.dao;

import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

import org.hibernate.Criteria;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.apotik.legenda.model.Purchases;
import com.apotik.legenda.model.Supplier;
import com.apotik.legenda.util.HibernateUtil;

public class PurchaseDao {

	public PurchaseDao() {
		// TODO Auto-generated constructor stub
	}
	
	public boolean saveOrUpdate(Purchases obj){
		try {
			Session session = HibernateUtil.getSessionFactory().openSession();
			session.beginTransaction();
			//session.save(product);
			session.saveOrUpdate(obj);
			session.getTransaction().commit();
			session.close();
			return true;
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, e.getMessage(), "Exception", JOptionPane.ERROR_MESSAGE);
			return false;
		}
	}
	
	public boolean delete(Purchases obj){
		try {
			Session session = HibernateUtil.getSessionFactory().openSession();
			
			session.beginTransaction();
			session.delete(obj);
			session.getTransaction().commit();
			session.close();
			
			return true;
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, e.getMessage(), "Exception", JOptionPane.ERROR_MESSAGE);
			return false;
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<Purchases> listByAnyCriteria(String searchValue, int first, int max){
		List<Purchases> list = new ArrayList<>();
		
		try {
			Session session = HibernateUtil.getSessionFactory().openSession();
			
			Criteria cr = session.createCriteria(Purchases.class);
			
			if(!searchValue.trim().equalsIgnoreCase("")){
				/*Criterion _code = Restrictions.ilike("invoiceNo", "%"+searchValue+"%");
				Criterion _name = Restrictions.ilike("productName", "%"+searchValue+"%");
				LogicalExpression orExp = Restrictions.or(_code, _name);
				cr.add(orExp);*/
				cr.add(Restrictions.ilike("invoiceNo", "%"+searchValue+"%"));
			}
			
			cr.setFirstResult(first);
			cr.setMaxResults(max);
			cr.addOrder(Order.desc("purchasesId"));
			list = cr.list();
			
			session.close();
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, e.getMessage(), "Exception", JOptionPane.ERROR_MESSAGE);
		}
		
		return list;
	}
	
	
	@SuppressWarnings("unchecked")
	public Purchases findBySupplierAndInvoiceNo(Supplier supplier, String invoiceNo){
		Purchases purchase = null;
		try {
			Session session = HibernateUtil.getSessionFactory().openSession();
			
			Criteria cr = session.createCriteria(Purchases.class);
			cr.add(Restrictions.eq("supplier", supplier));
			cr.add(Restrictions.eq("invoiceNo", invoiceNo));
			
			List<Purchases> list = cr.list();
			purchase = list.get(0);
			
			session.close();
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, e.getMessage(), "Exception", JOptionPane.ERROR_MESSAGE);
		}
		
		return purchase;
	}
	
	
	@SuppressWarnings({ "rawtypes" })
	public int size(String searchValue){
		int result = 0;
		
		try {
			Session session = HibernateUtil.getSessionFactory().openSession();
			Criteria cr = session.createCriteria(Purchases.class);
			
			if(!searchValue.trim().equalsIgnoreCase("")){
				/*Criterion _code = Restrictions.ilike("productCode", "%"+searchValue+"%");
				Criterion _name = Restrictions.ilike("productName", "%"+searchValue+"%");
				LogicalExpression orExp = Restrictions.or(_code, _name);
				cr.add(orExp);*/
				cr.add(Restrictions.ilike("invoiceNo", "%"+searchValue+"%"));
			}
			
			cr.setProjection(Projections.rowCount());
			
			List list = cr.list();
			
			if(list!=null){
				result = (int) list.get(0);
			}
			
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, e.getMessage(), "Exception", JOptionPane.ERROR_MESSAGE);
		}
		
		return result;
	}
	
}
